package com.liang.cloud.Service.impl;


import com.liang.cloud.Dto.CartDTO;
import com.liang.cloud.Emun.ProductStatusEnum;
import com.liang.cloud.Emun.ResultEnum;
import com.liang.cloud.Entity.ProductInfo;
import com.liang.cloud.Exception.SellException;
import com.liang.cloud.Repository.ProductInfoRepository;
import com.liang.cloud.Service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: 余亮亮
 * @description:
 * @create: 2019-12-28 14:02:35
 **/
@Service
@Transactional
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository repository;
    @Override
    public ProductInfo findOne(String productId) {
        return repository.findByProductId(productId);
    }

    @Override
    public List<ProductInfo> findUp() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        Page<ProductInfo> productInfoPage = repository.findAll(pageable);
        return productInfoPage;
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    public void addStore(List<CartDTO> list) {
        for (CartDTO cartDTO : list){
            ProductInfo productInfo = repository.findByProductId(cartDTO.getProductId());
            if (productInfo==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            productInfo.setProductStock(productInfo.getProductStock()+cartDTO.getProductQuantity());
            repository.save(productInfo);
        }

    }

    @Override
    public void decreaseStore(List<CartDTO> list) {

        for (CartDTO cartDTO: list) {
            ProductInfo productInfo = repository.findByProductId(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);

            //增加已售数量
            productInfo.setProductSold(productInfo.getProductSold()+cartDTO.getProductQuantity());

            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo upSale(String productId) {
        ProductInfo productInfo = repository.findByProductId(productId);
        if (productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if (!productInfo.getProductStatus().equals(ProductStatusEnum.DOWN.getCode())){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        repository.save(productInfo);
        return productInfo;
    }

    @Override
    public ProductInfo downSale(String productId) {
        ProductInfo productInfo = repository.findByProductId(productId);
        if (productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if (!productInfo.getProductStatus().equals(ProductStatusEnum.UP.getCode())){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        repository.save(productInfo);
        return productInfo;
    }
}
